package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.SkuOrderDetailPeriodizationQuery;
import com.coffee.order.api.sku.result.SkuOrderDetailPeriodizationResult;
import com.coffee.order.api.sku.service.SkuOrderDetailPeriodizationService;
import com.coffee.order.service.sku.bean.SkuOrderDetailPeriodization;
import com.coffee.order.service.sku.dao.SkuOrderDetailPeriodizationDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class SkuOrderDetailPeriodizationServiceImpl implements SkuOrderDetailPeriodizationService {
	@Resource
	private SkuOrderDetailPeriodizationDao	skuOrderDetailPeriodizationDao;

	@Override
	public Page<SkuOrderDetailPeriodizationResult> getPager(SkuOrderDetailPeriodizationQuery param, PagerInfo pr) {
		Page<SkuOrderDetailPeriodizationResult> pager=new Page<SkuOrderDetailPeriodizationResult>(pr.getStart(), pr.getPageSize());
		Integer count=skuOrderDetailPeriodizationDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<SkuOrderDetailPeriodization> list=skuOrderDetailPeriodizationDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, SkuOrderDetailPeriodizationResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(SkuOrderDetailPeriodizationQuery param){
		Integer count=skuOrderDetailPeriodizationDao.count(param);
		return count;
	}
	
	@Override
	public SkuOrderDetailPeriodizationResult getDetail(Integer id){
		SkuOrderDetailPeriodization item=skuOrderDetailPeriodizationDao.findById(id);
		if(item==null){
			return null;
		}
		SkuOrderDetailPeriodizationResult result=new SkuOrderDetailPeriodizationResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<SkuOrderDetailPeriodizationResult> getList(SkuOrderDetailPeriodizationQuery param, PagerInfo pr){
		List<SkuOrderDetailPeriodization> list=skuOrderDetailPeriodizationDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, SkuOrderDetailPeriodizationResult.class);
	}
	
	@Override
	public List<SkuOrderDetailPeriodizationResult> getList(SkuOrderDetailPeriodizationQuery param){
		List<SkuOrderDetailPeriodization> list=skuOrderDetailPeriodizationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuOrderDetailPeriodizationResult.class);
	}
	
	@Override
	public List<SkuOrderDetailPeriodizationResult> getList(){
		SkuOrderDetailPeriodizationQuery param = new SkuOrderDetailPeriodizationQuery();
		List<SkuOrderDetailPeriodization> list=skuOrderDetailPeriodizationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuOrderDetailPeriodizationResult.class);
	}


	@Override
	public void delete(Integer id) {
		skuOrderDetailPeriodizationDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(SkuOrderDetailPeriodizationResult result)throws BusinessException {
		SkuOrderDetailPeriodization item=new SkuOrderDetailPeriodization();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			skuOrderDetailPeriodizationDao.insert(item);
		}else {
			SkuOrderDetailPeriodization tmp=skuOrderDetailPeriodizationDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			skuOrderDetailPeriodizationDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,SkuOrderDetailPeriodizationResult> getItemMap(Set<Integer> idSet){
		Map<Integer, SkuOrderDetailPeriodizationResult> map=new HashMap<Integer, SkuOrderDetailPeriodizationResult>();
		List<SkuOrderDetailPeriodization> list=skuOrderDetailPeriodizationDao.findByIds(StringUtil.setToList(idSet));
		List<SkuOrderDetailPeriodizationResult> resultList=DTOUtils.convertList(list, SkuOrderDetailPeriodizationResult.class);
		for(SkuOrderDetailPeriodizationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,SkuOrderDetailPeriodizationResult> getItemMap(){
		Map<Integer, SkuOrderDetailPeriodizationResult> map=new HashMap<Integer, SkuOrderDetailPeriodizationResult>();
		SkuOrderDetailPeriodizationQuery param=new SkuOrderDetailPeriodizationQuery();
		List<SkuOrderDetailPeriodization> list=skuOrderDetailPeriodizationDao.page(param, 0, Integer.MAX_VALUE);
		List<SkuOrderDetailPeriodizationResult> resultList=DTOUtils.convertList(list, SkuOrderDetailPeriodizationResult.class);
		for(SkuOrderDetailPeriodizationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
